package dao;

import entity.Group;
import entity.User;
import groupInformation.Announcement;
import groupInformation.Task;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class GroupDAO {


    /**
     *  搜索所有小组
     * @author 藤树
     * */
    public static List<Group> select() throws SQLException {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        List<Group> groupList = new ArrayList<>();
        try {
//1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
            connection =
                    DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                            "root", "123456");
//3、获取数据库操作对象
            statement = connection.createStatement();
//4、执行SQL语句 JDBC中的SQL语句不以“;”结尾
            String Sql = "select * from Group_Information";
//ResultSet就是查询结果集对象，查询的结果都在这个对象当中
            resultSet = statement.executeQuery(Sql);



            while (resultSet.next()){
                Group group = new Group();


                String owner = resultSet.getString("owner");
                int id = resultSet.getInt("id");
                int owner_id = resultSet.getInt("owner_id");
                String name = resultSet.getString("name");
                String profile = resultSet.getString("profile");
                String task = resultSet.getString("task");
                String announcement = resultSet.getString("announcement");
                String tag = resultSet.getString("tag");
                String create_time = resultSet.getString("create_time");

               // System.out.println(owner+","+id+","+owner_id+","+name+","+profile+","+","+task+","+announcement+","+tag+","+create_time);
                group.setOwner(owner);
                group.setId(id);
                group.setOwner_id(owner_id);
                group.setName(name);
                group.setProfile(profile);
                group.setTask(task);
                group.setAnnouncement(announcement);
                group.setTag(tag);
                groupList.add(group);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
        return groupList;
    }
    /**添加tag
     * @author Pisces
     * */
    public static boolean tag(int id, String tag) throws  SQLException  {
        Statement statement = null;
        ResultSet resultSet = null;
        Connection connection = null;
        try {
            //获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system",
                    "root", "123456");
            //获取操作对象
            statement = connection.createStatement();

            String sql="select tag  from  Group_Information where id='"+id+"' " ;
            resultSet=statement.executeQuery(sql);
            if (resultSet.next()){
                String Tag= resultSet.getString("tag"); //获取标签
                if (Tag.equals(tag)){  //判断获取到的标签与输入的标签是否相同  相同原地更新一下   不同把获取的标签修改为 输入的标签
                    String sql1="update  Group_Information set  tag='" + tag + "' where id ='" + id + "'";
                    statement.executeUpdate(sql1);
                }else {
                    String sql2="update  Group_Information set  tag='" + tag + "' where id ='" + id + "'";
                    statement.executeUpdate(sql2);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        return true;
    }

    /**
     *  通过id搜索特定小组
     *  @author 藤树
     *  */
    public static List<Group> select(String group_id)throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        Group group = new Group();
        List<Group> groupList = new ArrayList<>();

        try {
            //1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");
            //3、获取数据库操作对象,并执行SQL语句 JDBC中的SQL语句不以“;”结尾
            ps = connection.prepareStatement("select * from Group_Information where id = ?");
            try{int groupId = Integer.parseInt(group_id);
                ps.setObject(1, groupId);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            //ResultSet就是查询结果集对象，查询的结果都在这个对象当中

            resultSet = ps.executeQuery();

            boolean next = resultSet.next();

            String owner = resultSet.getString("owner");
            int id = resultSet.getInt("id");
            int owner_id = resultSet.getInt("owner_id");
            String name = resultSet.getString("name");
            String profile = resultSet.getString("profile");
            String task = resultSet.getString("task");
            String announcement = resultSet.getString("announcement");
            String tag = resultSet.getString("tag");
            String create_time = resultSet.getString("create_time");

            System.out.println(owner+","+id+","+owner_id+","+name+","+profile+","+","+task+","+announcement+","+tag+","+create_time);

                group.setOwner(owner);
                group.setId(id);
                group.setOwner_id(owner_id);
                group.setName(name);
                group.setProfile(profile);
                group.setTask(task);
                group.setAnnouncement(announcement);
                group.setTag(tag);


                groupList.add(group);

                return groupList;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
        System.out.println("搜索失败");
        return null;
    }


    /**
     *  通过name搜索特定小组
     *  @author 藤树
     *  */
    public static List<Group> selectName(String name)throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        List<Group> groupList = new ArrayList<>();

        try {
            //1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");
            //3、获取数据库操作对象,并执行SQL语句 JDBC中的SQL语句不以“;”结尾
            ps = connection.prepareStatement("select * from Group_Information where name = ?");
            try{
                ps.setObject(1, name);
            }  catch (SQLException e) {
                e.printStackTrace();
            }

            //ResultSet就是查询结果集对象，查询的结果都在这个对象当中

            resultSet = ps.executeQuery();

            while (resultSet.next()) {
                Group group = new Group();

                String owner = resultSet.getString("owner");
                int id = resultSet.getInt("id");
                int owner_id = resultSet.getInt("owner_id");
                String group_name = resultSet.getString("name");
                String profile = resultSet.getString("profile");
                String task = resultSet.getString("task");
                String announcement = resultSet.getString("announcement");
                String tag = resultSet.getString("tag");
                String create_time = resultSet.getString("create_time");

                System.out.println(owner + "," + id + "," + owner_id + "," + group_name + "," + profile + "," + "," + task + "," + announcement + "," + tag + "," + create_time);

                group.setOwner(owner);
                group.setId(id);
                group.setOwner_id(owner_id);
                group.setName(group_name);
                group.setProfile(profile);
                group.setTask(task);
                group.setAnnouncement(announcement);
                group.setTag(tag);

                groupList.add(group);
            }
            System.out.println(groupList);
            return groupList;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
        System.out.println("搜索失败");
        return null;
    }

    /**
     *  解散小组
     *  @author 藤树
     *  */

    public static boolean dissolution(int group_id)throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        PreparedStatement st = null;
        PreparedStatement Taskst = null;
        PreparedStatement Relationst = null;

        ResultSet resultSet = null;

        boolean res = false;//判断是否成功

        try {
//1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
            connection =
                    DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                            "root", "123456");
//3、获取数据库操作对象


            ps = connection.prepareStatement("delete from Group_Information where id = ?");
            st = connection.prepareStatement("delete from Group_announcement where group_id = ?");
            Taskst = connection.prepareStatement("delete from Group_Task where group_id = ?");
            Relationst = connection.prepareStatement("delete from Group_Relation where group_id = ?");


            ps.setObject(1, group_id);
            st.setObject(1,group_id);
            Taskst.setObject(1,group_id);
            Relationst.setObject(1,group_id);

            int resRelation,resInformation,resTask,resAnn = 0;
            resRelation = Relationst.executeUpdate();
            resAnn = st.executeUpdate();
            resTask = Taskst.executeUpdate();
            resInformation = ps.executeUpdate();

            if(resInformation == 1) res = true;

        } catch (SQLException e) {
            e.printStackTrace();
//            return true;
        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (st!=null){
                st.close();
            }
            if (Taskst!=null){
                Taskst.close();
            }
            if (Relationst!=null){
                Relationst.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
        return res;
    }

    /**
     * 退出小组
     * @author 藤树
     * */
    public static boolean exit(int user_id)throws SQLException {
        boolean res = false;//判断是否成功

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        int groupId = -1;//为后续获取并使用小组id，创建全局变量groupId

        int i = 0;

        //搜索用户所在的group_id
        try {
//1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");
//3、获取数据库操作对象

            /*
             * 先通过搜索，取得成员所在的列数
             * */

            ps = connection.prepareStatement("select *from Group_Relation where member_id1=? or member_id2=? or member_id3=? or member_id4=? or member_id5=? or member_id6=? or member_id7=? or member_id8=? or member_id9=? or member_id10=?");

            ps.setObject(1,user_id);
            ps.setObject(2,user_id);
            ps.setObject(3,user_id);
            ps.setObject(4,user_id);
            ps.setObject(5,user_id);
            ps.setObject(6,user_id);
            ps.setObject(7,user_id);
            ps.setObject(8,user_id);
            ps.setObject(9,user_id);
            ps.setObject(10,user_id);


            resultSet = ps.executeQuery();
            boolean n = resultSet.next();
            int order = 0;

            int owner = resultSet.getInt("owner");
            groupId = resultSet.getInt("group_id");

        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }

//获取成员所在列数
        try {
//1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");
//3、获取数据库操作对象

            /*
             * 先通过搜索，取得成员所在的列数
             * */

            ps = connection.prepareStatement("select *from Group_Relation where group_id=?");
            ps.setObject(1,groupId);
            resultSet = ps.executeQuery();
            boolean n = resultSet.next();
            int order = 0;

            int owner = resultSet.getInt("owner");
            int group_id = resultSet.getInt("group_id");
            //找到用户Id所在的member_id列数
            for(i = 1; i < 11; i++){
                order = resultSet.getInt("member_id"+i);
                if(order == user_id) break;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
        /*
         * 通过算得的列数member_id+i，将该成员所在数据替换为null
         * */
        try {
//1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");
//3、获取数据库操作对象

            ps = connection.prepareStatement("update Group_Relation set member_id? = null where group_id = ?");
            ps.setObject(1, i);
            ps.setObject(2, groupId);

            int resRelation = 0;
            resRelation = ps.executeUpdate();
            if(resRelation == 1) res = true;
        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }
        return res;
    }

    /**
     * 添加任务
     * @author 藤树
     * 1.搜索小组是否已建立任务
     * 2.没有建立，则插入；已建立，则更新
     * */
    public static boolean Task(Task task)throws SQLException {

        boolean res = false;//判断是否成功

        Connection connection = null;
        PreparedStatement ps = null;
        PreparedStatement st = null;
        ResultSet resultSet = null;
//用来判断小组是否已在task表
        boolean next = true;
//获取需要的参数
        String group_id = task.getGroup_id();
        String name = task.getName();
        String content = task.getContent();
        String EndTime = task.getEndtime();


    //    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//对时间参数转型，使其变成能被sql语句接受的timestamp类型
      //  java.util.Date endTime = null;

//        try {
//            endTime = sf.parse(EndTime);
//
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        java.sql.Timestamp endtime = new java.sql.Timestamp(endTime.getTime());

        try {
            //1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");


            //3、获取数据库操作对象,并执行SQL语句 JDBC中的SQL语句不以“;”结尾
            ps = connection.prepareStatement("select * from Group_Task where group_id = ?");
            try{
                int groupId = Integer.parseInt(group_id);
                ps.setObject(1, groupId);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
                next = false;
            }
            //ResultSet就是查询结果集对象，查询的结果都在这个对象当中
            try{
                resultSet = ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            next = resultSet.next();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }


//next = true 则group_task存在该组，进行更新操作；否则代表不存在，进行插入操作
        if(next) {
            try {
//1、获取驱动
                DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
                connection =
                        DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                                "root", "123456");
//3、获取数据库操作对象
//更改group_task表的同时，更新information表
                st = connection.prepareStatement("update Group_Task set content = ?,name = ? where group_id = ?");
                ps = connection.prepareStatement("update Group_Information set task = ? where id = ?");

                st.setObject(1, content);
                st.setObject(2, name);
                st.setObject(3, group_id);

                ps.setObject(1, content);
                ps.setObject(2,group_id);

                int resTask,resInf = 0;
                resTask = st.executeUpdate();
                resInf = ps.executeUpdate();

                if(resTask == 1) res = true;
            } catch (SQLException e) {
                e.printStackTrace();

            } finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
                if (resultSet != null) {
                    resultSet.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (st != null) {
                    st.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        }
        else{
            try {
//1、获取驱动
                DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
                connection =
                        DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                                "root", "123456");
//3、获取数据库操作对象
//更改group_task表的同时，更新information表
                st = connection.prepareStatement("insert into  Group_Task(name,endtime,content,group_id) values(?,?,?,?)");
                ps = connection.prepareStatement("update Group_Information set task = ? where id = ?");

                st.setObject(1, name);
                st.setObject(2, EndTime);
                st.setObject(3, content);
                st.setObject(4, group_id);

                ps.setObject(1,content);
                ps.setObject(2,group_id);

                int resTask,resInf = 0;
                resTask = st.executeUpdate();
                resInf = ps.executeUpdate();

                if(resTask == 1) res = true;

            } catch (SQLException e) {
                e.printStackTrace();

            } finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
                if (resultSet != null) {
                    resultSet.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (st != null) {
                    st.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        }
        return res;
    }

    /**
     * 添加公告
     * @author 藤树
     * 1.搜索小组是否已有公告
     * 2.没有，则插入；已有，则更新
     * */
    public static boolean announcement (Announcement announcement)throws SQLException {

        boolean res = false;

        Connection connection = null;
        PreparedStatement ps = null;
        PreparedStatement st = null;
        ResultSet resultSet = null;
//用来判断小组是否已在announcement表
        boolean next = true;
//获取需要的参数
        String group_id = announcement.getGroup_id();
        String Sponsor = announcement.getSponsor();
        String content = announcement.getContent();

        try {
            //1、获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                    "root", "123456");
            //3、获取数据库操作对象,并执行SQL语句 JDBC中的SQL语句不以“;”结尾

            ps = connection.prepareStatement("select * from Group_announcement where group_id = ?");
            try{
                int groupId = Integer.parseInt(group_id);
                ps.setObject(1, groupId);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
                next = false;
            }
            //ResultSet就是查询结果集对象，查询的结果都在这个对象当中

            try{
                resultSet = ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            next = resultSet.next();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//6、释放资源
//先关闭resultSet，再关闭Statement，最后关闭Connection
            if (resultSet!=null){
                resultSet.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }


//next = true 则group_task存在该组，进行更新操作；否则代表不存在，进行插入操作
        if(next) {
            try {
//1、获取驱动
                DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
                connection =
                        DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                                "root", "123456");
//3、获取数据库操作对象
//更改group_task表的同时，更新information表
                st = connection.prepareStatement("update Group_announcement set Sponsor = ? ,content = ?  where group_id = ?");
                ps = connection.prepareStatement("update Group_Information set announcement = ?  where id = ?");

                st.setObject(1, Sponsor);
                st.setObject(2, content);
                st.setObject(3,group_id);

                ps.setObject(1, content);
                ps.setObject(2, group_id);

                int resAnn,resInf = 0;
                resAnn = st.executeUpdate();
                resInf = ps.executeUpdate();

                if(resAnn == 1)    res = true;
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
//6、释放资源

                if (resultSet != null) {
                    resultSet.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (st != null) {
                    st.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        }
        else{
            try {
//1、获取驱动
                DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2、获取连接
                connection =
                        DriverManager.getConnection("jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC",
                                "root", "123456");
//3、获取数据库操作对象
//更改group_task表的同时，更新information表
                st = connection.prepareStatement("insert into  Group_announcement(Sponsor,group_id,content) values(?,?,?)");
                ps = connection.prepareStatement("update Group_Information set announcement = ? where id = ?");

                st.setObject(1, Sponsor);
                st.setObject(2, group_id);
                st.setObject(3, content);

                ps.setObject(1,content);
                ps.setObject(2,group_id);

                int resAnn,resInf = 0;
                resAnn = st.executeUpdate();
                resInf = ps.executeUpdate();

                if(resAnn == 1)    res = true;
            } catch (SQLException e) {
                e.printStackTrace();

            } finally {
//6、释放资源

                if (ps != null) {
                    ps.close();
                }
                if (st != null) {
                    st.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        }
        return res;
    }

    /**
     * queryModInGroup
     * @param type char 搜索对象
     * @param strings String[] 搜索关键词
     * @return boolean
     */
    public boolean query(char type, String ...strings) {
        //进入查询
        boolean res = false;//返回值
        Connection connection = null;//连接
        PreparedStatement ps  = null;//操作对象
        ResultSet resultSet = null;//结果集
        try {
            //1.获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //2.获取连接
            connection = DriverManager.getConnection("jdbc:mysql://82.156.38.140/team_management_system", "root", "123456");
            //3.获取数据库操作对象
            String sql = "";
            switch (type){
                case 'I':
                    sql = "SELECT * FROM Group_Information WHERE id = ?";
                    ps = connection.prepareStatement(sql);
                    ps.setString(1, strings[0]);
                    break;
                case 'N':
                    sql = "SELECT * FROM Group_Information WHERE name LIKE ?";
                    ps = connection.prepareStatement(sql);
                    ps.setString(1, "%"+strings[0]+"%");
                    break;
            }
            //4.执行SQL语句
            resultSet = ps.executeQuery();
            //5.处理查询结果集
            if(resultSet.next())res=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            //从小到大分别释放资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            }catch(SQLException e){e.printStackTrace();}
            try {
                if (ps != null) {
                    ps.close();
                }
            }catch(SQLException e){e.printStackTrace();}
            try {
                if (connection != null) {
                    connection.close();
                }
            }catch (SQLException e){e.printStackTrace();}
        }
        return res;
    }


    /**
     * 修改小组内容
     * @author 风
     * true: 更新成功
     * false: 更新失败
     */
    public static boolean updateTeam(Group group){
        boolean status = false;

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        final String URL = "jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC";
        final String USER = "root";
        final String PWD = "123456";

        try{
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            conn = DriverManager.getConnection(URL, USER, PWD);
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            String sql = "UPDATE Group_Information SET name = ?, profile = ? where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, group.getName());
            pstmt.setString(2, group.getProfile());
            pstmt.setInt(3, group.getId());

            int row = pstmt.executeUpdate();
            if (row > 0){
                status = true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if (pstmt != null){
                    pstmt.close();
                }
                if (conn != null){
                    conn.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        return status;
    }

    /**
     * 传入用户信息，返回小组信息
     * author 风
     * 通过用户昵称，查询小组信息
     */

    public static Group GainTeam(User user){
        Group group = null;

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        final String URL = "jdbc:mysql://82.156.38.140:3306/team_management_system? &useSSL=false&serverTimezone=UTC";
        final String USER = "root";
        final String PWD = "123456";

        try{
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            conn = DriverManager.getConnection(URL, USER, PWD);
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            String sql = "select * from Group_Information where owner_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getId());

            rs = pstmt.executeQuery();

            if (rs.next() && rs != null){
                group = new Group();
                group.setOwner(rs.getString("owner"));
                group.setOwner_id(rs.getInt("owner_id"));
                group.setId(rs.getInt("id"));
                group.setName(rs.getString("name"));
                group.setProfile(rs.getString("profile"));
                group.setTask(rs.getString("task"));
                group.setAnnouncement(rs.getString("announcement"));
                group.setTag(rs.getString("tag"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if (pstmt != null){
                    pstmt.close();
                }
                if (conn != null){
                    conn.close();
                }
                if (rs != null){
                    conn.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        return group;
    }
    /**
     * 更新小组任务
     * @author 藤树
     * 1.update 更新小组任务信息
     * */
    public static boolean updateTask(int group_id,String taskContext){
        String url = "jdbc:mysql://82.156.38.140:3306/team_management_system?autoReconnect=TRUE&serverTimezone=UTC";
        String sqlUser = "root";
        String sqlPassword = "123456";
        Connection connection = null;
        PreparedStatement pstm = null;
        String sql = "UPDATE Group_Information SET task = ?" +
                " WHERE id=?";
        int i = 0;
        try {
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            connection = DriverManager.getConnection(url, sqlUser, sqlPassword);
            pstm = connection.prepareStatement(sql);
            pstm.setString(1,taskContext);
            pstm.setInt(2,group_id);
            i = pstm.executeUpdate();
            System.out.println("GroupDao updateTask() --> 成功修改了" + i + "条数据！");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstm != null) {// pstm不为空时才关闭
                    pstm.close();
                }
                if (connection != null) {// 同上
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i == 1?true:false;
    }
}
